package com.ruoyi.business.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.mapper.SysDictDataMapper;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.system.service.ISysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: ygw
 * @Date: 2022/09/28/18:14
 * @Description:
 */
@RestController
@RequestMapping("/pdl/dict/data")
public class PdlDictDataController {

    @Autowired
    private SysDictDataMapper dictDataMapper;
    /**
     * 业务系统 清空缓存
     */
    @DeleteMapping("/clearDictCache")
    public AjaxResult clearDictCache() {
        DictUtils.clearDictCache();
        return AjaxResult.success();
    }

    /**
     * 设置缓存
     * @param sysDictData
     * @return
     */
    @PostMapping("/setCache")
    public AjaxResult setCache (@RequestBody SysDictData sysDictData) {
        List<SysDictData> dictDatas = DictUtils.getDictCache(sysDictData.getDictType());
        if (StringUtils.isEmpty(dictDatas)) {
            dictDatas = dictDataMapper.selectDictDataByType(sysDictData.getDictType());
            DictUtils.setDictCache(sysDictData.getDictType(), dictDatas);
        }
        return AjaxResult.success();
    }

    /**
     * 清除指定 key的缓存
     * @param key
     * @return
     */
    @PostMapping("/clearDictCacheByKey")
    public AjaxResult clearDictCacheByKey (@RequestBody String key) {
        DictUtils.clearDictCacheByKey(key);
        return AjaxResult.success();
    }
}
